﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Data;
using Maticsoft.Common;


namespace WEB.workflow
{
    public partial class countEmpWorkFlow : System.Web.UI.Page
    {
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { BindJobData(ddlJob); }
            if (ds != null)
            {
                gvEmpCount.DataSource = ds;
                gvEmpCount.DataBind();
            }

            if (!Page.IsPostBack)
            {
                this.tbMonthPicker.Attributes.Add("onClick", "new WdatePicker({skin:'whyGreen',dateFmt:'yyyy年MM月'})");//WdatePicker() 
            }
            
            
        }

        protected void btnCount_Click(object sender, EventArgs e)
        {
            int[] monthDay;
            if (tbMonthPicker.Text.Trim() == "")
            {
                MessageBox.Show(this, "尚未选择日期.");
                return;
            }
            DateTime dateStart=Convert.ToDateTime(tbMonthPicker.Text);
            DateTime dateEnd=dateStart;
            int year=dateStart.Year;
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))  //闰年
            {
                monthDay = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }
            else
            {
                monthDay = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }
            dateEnd=dateEnd.AddDays(monthDay[dateStart.Month - 1] - 1);
            dateEnd = dateEnd.AddHours(23.999999);
            WorkflowHandleManage bll=new WorkflowHandleManage();
            ds = bll.GetCountView(string.Format("doEmpNo='{0}' and doDate between '{1}' and '{2}'", ddlEmp.SelectedValue,dateStart,dateEnd));
            gvEmpCount.DataSource = ds;
            gvEmpCount.DataBind();
            lblInfo.Text = string.Format("该员工在{0}的业务合计：{1}（单）", tbMonthPicker.Text, ds.Tables[0].Rows.Count);
        }

        /// <summary>
        /// 绑定员工列表
        /// </summary>
        /// <param name="ddl"></param>
        private void BindEmpData(DropDownList ddl, string jobNo)
        {
            if (ddl.Items.Count > 0) ddl.Items.Clear();
            UserManage userBll = new UserManage();
            List<Users> userModels = userBll.GetModelList("");
            foreach (Users aUser in userModels)
            {
                if(aUser.JobNo == jobNo)
                    ddl.Items.Add(new ListItem(aUser.UserName, aUser.EmpNum.ToString()));
            }
        }

        /// <summary>
        /// 绑定岗位
        /// </summary>
        /// <param name="ddl"></param>
        private void BindJobData(DropDownList ddl)
        {
            //ddl.Items.Clear();
            JobManage jobBll = new JobManage();
            List<Info_job> userModels = jobBll.GetModelList("");
            foreach (Info_job aJob in userModels)
            {
                ddl.Items.Add(new ListItem(aJob.jobName,aJob.jobNo));
            }
        }

        protected void gvEmpCount_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                WorkflowHandleManage bll = new WorkflowHandleManage();
                //CustomsSystem.Model.workflow model;
                int sheetID = Convert.ToInt32(Request.QueryString["ID"]);
                int IntimeFlag;
                /*if (e.Row.RowIndex == ds.Tables[0].Rows.Count - 1)//如果是当前环节
                {
                    IntimeFlag = bll.GetIntime(sheetID);//判断及时度
                }
                else*/   //读取就好
                {
                    int wfID = Convert.ToInt32(gvEmpCount.DataKeys[e.Row.RowIndex].Value);
                    CustomsSystem.Model.workflow model = new CustomsSystem.Model.workflow();
                    model = bll.GetModel(wfID);
                    if (model.ontimeType != null) IntimeFlag = (int)model.ontimeType;
                    else IntimeFlag = 0;
                }

                Image tmp_ImageIntime = (Image)e.Row.Cells[6].FindControl("ImgIntime");//获得图片列的控件
                if (tmp_ImageIntime != null)
                {
                    string imagepath = "";
                    if (IntimeFlag == 1)
                    {
                        imagepath = "../images/status/Green.png";
                    }
                    else if (IntimeFlag == 2)
                    {
                        imagepath = "../images/status/Yellow.png";
                    }
                    else
                    {
                        imagepath = "../images/status/Red.png";
                    }
                    tmp_ImageIntime.ImageUrl = imagepath;//显示图片
                }
            }
        }

        protected void ddlJob_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindEmpData(ddlEmp, ddlJob.SelectedValue);
        }
    }
}